import * as VueRouter from 'vue-router'
import { routes } from './routes'
import testTools from '@test/tools'

const initRouter = () => {

    const router = VueRouter.createRouter({
        history: VueRouter.createWebHistory(),
        routes
    })

    router.beforeEach((to, from, next) => {
        const userInfo = testTools.local.get('userInfo')
        if (to.fullPath === '/signin') {
            if (userInfo && userInfo.token) {
                return next('/home')
            }
        } else {
            if (!userInfo || !userInfo.token) {
                return next('/signin')
            }
        }
        next()
    })

    return router

}

export default initRouter